package cn.btks.resultintegration.statics.controller;

import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.statics.service.StaticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author lijun
 * @Date 2021/2/4 11:16
 */
@RestController
@RequestMapping("api/v1.0/resultIntegration/statics")
@Api(tags = "统计模块")
@Validated
public class StaticsController {

    @Autowired
    StaticsService staticsService;

    @GetMapping("/queryUnitAreaAndCount")
    @ApiOperation(value = "统计查询环境管控单元数量及面积",notes = "firstArea-优先面积，secondArea-重点面积，thirdArea-一般面积//firstCount-优先数量，secondCount-重点数量，thirdCount-一般数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState queryUnitAreaAndCount() {
        Map<String, Object> map = staticsService.queryUnitAreaAndCount();
        return map != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), map) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/queryCityUnitCount")
    @ApiOperation(value = "统计各个师查询环境管控单元数量",notes = "city-师名称，firstCount-优先数量，secondCount-重点数量，thirdCount-一般数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState queryCityUnitCount() {
        List<Map<String, Object>> mapList = staticsService.queryCityUnitCount();
        return mapList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), mapList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/queryCityUnitAreaAndCount/{city}")
    @ApiOperation(value = "统计师环境管控单元面积和数量",notes = "city-师名称，firstArea-优先面积，secondArea-重点面积，thirdArea-一般面积//firstCount-优先数量，secondCount-重点数量，thirdCount-一般数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "city", value = "师名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState queryCityUnitAreaAndCount(@PathVariable("city") String city) {
        Map<String, Object> map = staticsService.queryCityUnitAreaAndCount(city);
        return map != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), map) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/queryCountyUnitCount/{city}")
    @ApiOperation(value = "统计各个团查询环境管控单元数量",notes = "city-师名称，county-团名称，firstCount-优先数量，secondCount-重点数量，thirdCount-一般数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "city", value = "师名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState queryCountyUnitCount(@PathVariable("city") String city) {
        List<Map<String, Object>> mapList = staticsService.queryCountyUnitCount(city);
//        Map<String, Object> map = staticsService.queryCityUnitAreaAndCount(city);
//        if(MapUtils.isNotEmpty(map)) {
//            mapList.add(map);
//        }
        return mapList != null ? new ResultState(StateInfo.FIND_OK.getCode(),StateInfo.FIND_OK.getMsg(), mapList) :
                new ResultState(StateInfo.FIND_NULL.getCode(),StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/queryAchievementCountAndTime")
    @ApiOperation(value = "统计成果申报总数及最近填报时间",notes = "count-申报总数，currentDate-最近时间")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "所属师ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState queryAchievementCountAndTime(Long cityId, HttpServletRequest request) throws Exception {
        Map<String, Object> map = staticsService.queryAchievementCountAndTime(cityId, request);
        return map != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), map) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }
}
